<?php

namespace app\modules\api\models;

use Yii;
/**
* @OA\Schema(
*      schema="Position",
*      required={"group_id","dept_id","name","created_at","updated_at"},
*     @OA\Property(
*        property="id",
*        description="主键",
*        type="integer",
*        format="int64",
*    ),
*     @OA\Property(
*        property="group_id",
*        description="组织",
*        type="integer",
*        format="int64",
*    ),
*     @OA\Property(
*        property="dept_id",
*        description="部门",
*        type="integer",
*        format="int64",
*    ),
*     @OA\Property(
*        property="parent_id",
*        description="上级",
*        type="integer",
*        format="int64",
*        default="0",
*    ),
*     @OA\Property(
*        property="name",
*        description="名称",
*        type="string",
*        maxLength=32,
*    ),
* )
*/

/**
 * This is the model class for table "{{%position}}".
 *
 * @property int $id 主键
 * @property int $group_id 组织
 * @property int $dept_id 部门
 * @property int $parent_id 上级
 * @property string $name 名称
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class Position extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            [
                'class' => \yii2tech\ar\softdelete\SoftDeleteBehavior::className(),
                'softDeleteAttributeValues' => [
                    'deleted_at' =>  time(),
                ],
                'restoreAttributeValues' => [
                    'deleted_at' => 0
                ]
//                'replaceRegularDelete' => true
            ],
            [
                'class' => \yii\behaviors\TimestampBehavior::className(),
                'createdAtAttribute' => 'created_at',
                'updatedAtAttribute' => 'updated_at',
                'value' =>  time(),
            ],
            [
                'class' => \yii\behaviors\BlameableBehavior::className(),
                'createdByAttribute' => 'created_by',
                'updatedByAttribute' => 'updated_by',
            ],
        ];
    }
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%position}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['group_id', 'dept_id', 'name', 'created_at', 'updated_at'], 'required'],
            [['group_id', 'dept_id', 'parent_id', 'created_at', 'updated_at'], 'integer'],
            [['name'], 'string', 'max' => 32],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => '主键',
            'group_id' => '组织',
            'dept_id' => '部门',
            'parent_id' => '上级',
            'name' => '名称',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }


    public static function find()
    {
    $query = parent::find();

    $query->attachBehavior('softDelete', \yii2tech\ar\softdelete\SoftDeleteQueryBehavior::className());

    return $query->notDeleted();
    }

    public function fields()
    {
        $fields = parent::fields();
        $customFields = [
            'created_at' => function ($model) {
                return \Yii::$app->formatter->asDatetime($model->created_at,'php:c');
            },
            'updated_at' => function ($model) {
                return \Yii::$app->formatter->asDatetime($model->updated_at,'php:c');
            },
        ];
        unset($fields['deleted_at']);

        return \yii\helpers\ArrayHelper::merge($fields, $customFields);
    }

    public function extraFields()
    {
        return [
            'creator',
            'updater'
        ];
    }

    public function getCreator()
    {
        return $this->hasOne(User::className(), ['id' => 'created_by']);
    }

    public function getUpdater()
    {
        return $this->hasOne(User::className(), ['id' => 'updated_by']);
    }

}
